package com.qingke.easyjava.springhibernate.servlet;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.qingke.easyjava.springhibernate.bean.Clazz;
import com.qingke.easyjava.springhibernate.bean.Student;
import com.qingke.easyjava.springhibernate.service.StudentService;

/**
 * Servlet implementation class StudentServlet
 */
public class StudentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StudentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
		StudentService ss = wac.getBean(StudentService.class);
		Student s = new Student();
		s.setName("周翔");
		s.setAge(21);
		s.setHeight(172.0);
		Clazz clazz = new Clazz();
		clazz = ss.getClazzByName("一班");
		s.setClazz(clazz);
		ss.saveStudent(s);
//		clazz.setName("三班");
//		clazz.setId(3);

//		ss.saveClazz(clazz);
		OutputStream os = response.getOutputStream();
	    os.write("Success".getBytes());
	    os.flush();
	    os.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
